import 'package:agora_rtm/src/impl/gen/rtm_result_handler.dart';
import 'package:agora_rtm/src/impl/extensions.dart';
import 'package:agora_rtm/src/binding_forward_export.dart';
import 'package:agora_rtm/src/bindings/gen/agora_rtm_lock_impl.dart'
    as native_binding;
import 'package:flutter/services.dart';

import 'agora_rtm_client_ohos_impl.dart';

class RtmLockOhosImpl implements RtmLock {
  RtmLockOhosImpl(this._methodChannel,this.rtmResultHandler);
  final MethodChannel _methodChannel;
  final RtmResultHandler rtmResultHandler;

  @override
  Future<(RtmStatus, AcquireLockResult?)> acquireLock(String channelName, RtmChannelType channelType, String lockName, {bool retry = false}) async {
    final Map<String, dynamic> args = {
      "channelName":channelName,
      "channelType":channelType.value(),
      "lockName":lockName,
      "retry":retry,
    };
    var code = await _methodChannel.invokeMethod("acquireLock", args);
    Map<String,dynamic> map = jsonDecode(code!);
    final (AcquireLockResult result, RtmErrorCode errorCode) = await rtmResultHandler.request(int.parse(map['requestId']));
    return (RtmClientOhosImpl.getRtmStatus(map['errorCode']),result);
    // return (RtmClientOhosImpl.getRtmStatus(code),AcquireLockResult(channelName: channelName,channelType: channelType,lockName: lockName,errorDetails: ""));
  }

  @override
  Future<(RtmStatus, GetLocksResult?)> getLocks(String channelName, RtmChannelType channelType) async {
    final Map<String, dynamic> args = {
      "channelName":channelName,
      "channelType":channelType.value(),
    };
    var code = await _methodChannel.invokeMethod("getLocks", args);
    Map<String,dynamic> map = jsonDecode(code!);
    final (GetLocksResult result, RtmErrorCode errorCode) = await rtmResultHandler.request(int.parse(map['requestId']));
    return (RtmClientOhosImpl.getRtmStatus(map['errorCode']),result);
  }

  @override
  Future<(RtmStatus, ReleaseLockResult?)> releaseLock(String channelName, RtmChannelType channelType, String lockName) async {
    final Map<String, dynamic> args = {
      "channelName":channelName,
      "channelType":channelType.value(),
      "lockName":lockName,
    };
    var code = await _methodChannel.invokeMethod("releaseLock", args);
    Map<String,dynamic> map = jsonDecode(code!);
    final (ReleaseLockResult result, RtmErrorCode errorCode) = await rtmResultHandler.request(int.parse(map['requestId']));
    return (RtmClientOhosImpl.getRtmStatus(map['errorCode']),result);
  }

  @override
  Future<(RtmStatus, RemoveLockResult?)> removeLock(String channelName, RtmChannelType channelType, String lockName) async {
    final Map<String, dynamic> args = {
      "channelName":channelName,
      "channelType":channelType.value(),
      "lockName":lockName,
    };
    var code = await _methodChannel.invokeMethod("removeLock", args);
    Map<String,dynamic> map = jsonDecode(code!);
    final (RemoveLockResult result, RtmErrorCode errorCode) = await rtmResultHandler.request(int.parse(map['requestId']));
    return (RtmClientOhosImpl.getRtmStatus(map['errorCode']),result);
  }

  @override
  Future<(RtmStatus, RevokeLockResult?)> revokeLock(String channelName, RtmChannelType channelType, String lockName, String owner) async {
    final Map<String, dynamic> args = {
      "channelName":channelName,
      "channelType":channelType.value(),
      "lockName":lockName,
      "owner":owner,
    };
    var code = await _methodChannel.invokeMethod("revokeLock", args);
    Map<String,dynamic> map = jsonDecode(code!);
    final (RevokeLockResult result, RtmErrorCode errorCode) = await rtmResultHandler.request(int.parse(map['requestId']));
    return (RtmClientOhosImpl.getRtmStatus(map['errorCode']),result);
  }

  @override
  Future<(RtmStatus, SetLockResult?)> setLock(String channelName, RtmChannelType channelType, String lockName, {int ttl = 10}) async {
    final Map<String, dynamic> args = {
      "channelName":channelName,
      "channelType":channelType.value(),
      "lockName":lockName,
      "ttl":ttl,
    };
    var code = await _methodChannel.invokeMethod("revokeLock", args);
    Map<String,dynamic> map = jsonDecode(code!);
    final (SetLockResult result, RtmErrorCode errorCode) = await rtmResultHandler.request(int.parse(map['requestId']));
    return (RtmClientOhosImpl.getRtmStatus(map['errorCode']),result);
  }

}
